package com.ruoyi.extdata.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.bizsys.domain.*;
import com.ruoyi.bizsys.service.*;
import com.ruoyi.bizsys.utils.BizConstants;
import com.ruoyi.common.enums.RspEnmu;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.extdata.common.Constants;
import com.ruoyi.system.service.ISysDictDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Author: Jamnsin
 * @Date: 2021/3/10 6:12 下午
 */
@Slf4j
public class BaseService {

    @Autowired
    IAppUserService appUserService;

    @Autowired
    public ICustomerInfoService customerInfoService;

    @Autowired
    ISysDictDataService sysDictDataService;

    @Autowired
    IExtReqLogService extReqLogService;

    @Autowired
    IRealNameAuthService realNameAuthService;

    @Autowired
    ICreditAccountService creditAccountService;

    @Autowired
    ICustBankCardService custBankCardService;

    protected String getCustNo(String userId) {
        return getCustInfoByUserId(userId).getCustNo();
    }

    protected AppUser getAppUserByCustNo(String custNo) {
        AppUser result = null;
        CustomerInfo customerInfo = new CustomerInfo();
        customerInfo.setCustNo(custNo);
        customerInfo.setState(BizConstants.BIZ_STATUS_1);
        List<CustomerInfo> customerInfoList = customerInfoService.selectCustomerInfoList(customerInfo);
        if (customerInfoList.size() == 1) {
            customerInfo = customerInfoList.get(0);
            AppUser appUser = new AppUser();
            appUser.setUserUuid(customerInfo.getUserId());
            appUser.setState("0");
            List<AppUser> appUserList = appUserService.selectAppUserList(appUser);
            if (appUserList.size() == 1) {
                result = appUserList.get(0);
            }
        }
        return result;
    }

    protected CustomerInfo getCustInfoByUserId(String userId) {
        CustomerInfo customerInfo = new CustomerInfo();
        customerInfo.setUserId(userId);
        customerInfo.setState(BizConstants.BIZ_STATUS_1);
        List<CustomerInfo> customerInfoList = customerInfoService.selectCustomerInfoList(customerInfo);
        if (CollectionUtils.isEmpty(customerInfoList)) {
            throw new BusinessException(RspEnmu.CUSTOMER_IS_NOT_EXISTS);
        }

        return customerInfoList.get(0);
    }

    protected CustomerInfo getCustInfo(String custNo) {
        CustomerInfo customerInfo = new CustomerInfo();
        customerInfo.setCustNo(custNo);
        customerInfo.setState(BizConstants.BIZ_STATUS_1);
        List<CustomerInfo> customerInfoList = customerInfoService.selectCustomerInfoList(customerInfo);
        if (CollectionUtils.isEmpty(customerInfoList)) {
            throw new BusinessException(RspEnmu.CUSTOMER_IS_NOT_EXISTS);
        }

        return customerInfoList.get(0);
    }

    protected AppUser getAppUser(String userId) {
        AppUser appUser = new AppUser();
        appUser.setUserUuid(userId);
        List<AppUser> appUsers = appUserService.selectAppUserList(appUser);
        if (CollectionUtils.isEmpty(appUsers)) {
            throw new BusinessException(RspEnmu.USER_NOT_EXIST);
        }

        return appUsers.get(0);
    }

    protected RealNameAuth getRealNameAuth(String userId) {
        RealNameAuth auth = new RealNameAuth();
        auth.setUserId(userId);
        auth.setState(BizConstants.BIZ_STATUS_1);
        List<RealNameAuth> realNameAuthList = realNameAuthService.selectRealNameAuthList(auth);
        RealNameAuth realNameAuth = realNameAuthList.get(0);
        return realNameAuth;
    }

    protected RealNameAuth getRealNameByCustNo(String custNo) {
        RealNameAuth auth = new RealNameAuth();
        auth.setCustNo(custNo);
        auth.setState(BizConstants.BIZ_STATUS_1);
        List<RealNameAuth> realNameAuthList = realNameAuthService.selectRealNameAuthList(auth);
        RealNameAuth realNameAuth = realNameAuthList.get(0);
        return realNameAuth;
    }

    protected CustBankCard getDefaultCardByCust(String cust) {
        CustBankCard custBankCard = new CustBankCard();
        custBankCard.setCustNo(cust);
        custBankCard.setIsDefault("Y");
        List<CustBankCard> custBankCards = custBankCardService.selectCustBankCardList(custBankCard);
        if (!CollectionUtils.isEmpty(custBankCards)) {
            throw new BusinessException(RspEnmu.CUSTOMER_IS_NOT_EXISTS);
        }
        return custBankCards.get(0);
    }

    protected CreditAccount getCreditAccount(String custNo) {
        List<CreditAccount> creditAccountList = getCreditAccounts(custNo);
        return creditAccountList.get(0);
    }

    private List<CreditAccount> getCreditAccounts(String custNo) {
        CreditAccount ca = new CreditAccount();
        ca.setCustNo(custNo);
        ca.setState(BizConstants.BIZ_STATUS_1);
        List<CreditAccount> creditAccountList = creditAccountService.selectCreditAccountList(ca);
        if (CollectionUtils.isEmpty(creditAccountList)) {
            throw new BusinessException(RspEnmu.CUSTOMER_IS_NOT_EXISTS);
        }
        return creditAccountList;
    }

    protected String getDictLable(String dictType, String dictValue) {
        return sysDictDataService.selectDictLabel(dictType, dictValue);
    }

    protected int updateAppUserById(AppUser appUser) {
        return appUserService.updateAppUser(appUser);
    }

}
